package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.beans.BGchat;
import com.sam.im.heiqun.uis.beans.BGsetAll;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/8/16.
 */

public class LookBGImage extends BaseSwipeBackActivity {

    //ok
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.image_big)
    ImageView image_big;

    String path ="";
    String toid ="";
    int type = 0 ;//0 - set中的设置，1-chat中的单独设置

    public static void startActivity(Activity activity, String path, String toid, int type){
        Intent intent = new Intent(activity,LookBGImage.class);
        intent.putExtra("path",path);
        intent.putExtra("type",type);
        intent.putExtra("toid",toid);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.look);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        ok.setText(R.string.use_bg);
        ok.setVisibility(View.VISIBLE);

        path = getIntent().getStringExtra("path");
        toid = getIntent().getStringExtra("toid");
        type = getIntent().getIntExtra("type",0);
        GlideUtils.loadImage(LookBGImage.this,path,image_big);
//image_big
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_look_bg_image;
    }

    @OnClick({R.id.ok})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ok :
                try {
                    if (0 == type){
                        List<BGsetAll> list = BGsetAll.find(BGsetAll.class,"creatid=?",new String[]{ToolsUtils.getMyUserId()});
                        if (null!=list && list.size() > 0){
                            BGsetAll mBGsetAll = list.get(0);
                            mBGsetAll.setBgpath(path);
                            mBGsetAll.save();
                        }else {
                            BGsetAll mBGsetAll = new BGsetAll();
                            mBGsetAll.setCreatid(ToolsUtils.getMyUserId());
                            mBGsetAll.setBgpath(path);
                            mBGsetAll.save();
                        }
                    }else {
                        List<BGchat> list = BGchat.find(BGchat.class,"creatid=? and sessionid=?",new String[]{ToolsUtils.getMyUserId(), toid});
                        if (null!=list && list.size() > 0){
                            BGchat mBGsetAll = list.get(0);
                            mBGsetAll.setBgchat(path);
                            mBGsetAll.save();
                        }else {
                            BGchat mBGsetAll = new BGchat();
                            mBGsetAll.setCreatid(ToolsUtils.getMyUserId());
                            mBGsetAll.setSessionid(toid);
                            mBGsetAll.setBgchat(path);
                            mBGsetAll.save();
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
finish();
                break;
        }

    }
}
